package com.plant.action;

import com.plant.entity.PgcArticle;
import com.plant.service.ArticleService;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Java工程师——TOMli
 * 			以下是PC端文章管理的接口
 * 			---涉及的表：表1：
 * 			  --secret	由 随机的字符串 和 密匙（token） 组成进行md5加密组成
				---	pust_val	随机生成的字符串。可以是时间戳或乱码字符
 */
@Controller                                                                
@RequestMapping(value = "/article")                                                
public class ArticleAction {
	
    @Resource(name = "articleServiceImpl")
    private ArticleService articleService;

	@RequestMapping(value = "newarticle")//--1.新建文章
    @ResponseBody                                             
    public Map<Object,Object> newarticle(PgcArticle pa,String secret,String pust_val,HttpServletRequest request,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pa.getUserId()==null||pa.getTitle()==null||pa.getContent()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
    	}else{
		    Md md=new Md();
		    String mds=md.md5calc(pust_val+"flower_duigu2016");
		    if(secret.endsWith(mds)){
				    SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    String t=sim.format(new Date());
		    		GetIp getip=new GetIp();
		    		String ip=getip.getIp2(request);
		    		pa.setIp(ip);
		    		pa.setAddTime(Timestamp.valueOf(t));
				    articleService.addarticle(pa);
				    map.put("state", "1");
				    map.put("message", "新建文章成功");
		    		return map; 
		    }else{
	    		map.put("message", "秘钥错误");
	    		map.put("state", "0");
	    		return map;
		    }
    	}
    }
	
	
	@RequestMapping(value = "uparticle")//--2.PC端修改文章
	@ResponseBody                                             
	public Map<Object,Object> uparticle(PgcArticle pa,String secret,String pust_val,HttpServletRequest request,HttpServletResponse response) throws IOException, FileUploadException {
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pa.getUserId()==null||pa.getTitle()==null||pa.getContent()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
    	}else{
		    Md md=new Md();
		    String mds=md.md5calc(pust_val+"flower_duigu2016");
		    if(secret.endsWith(mds)){
						PgcArticle p=(PgcArticle) articleService.oneinf(pa);
						pa.setAddTime(p.getAddTime());
						pa.setIp(p.getIp());
						boolean b=articleService.uparticle(pa);//修改文章
						if(b){
							map.put("state", "1");
							map.put("message", "修改文章成功");
							return map; 
						}else{
							map.put("state", "0");
							map.put("message", "修改文章失败");
							return map; 
						}
		    }else{
	    		map.put("message", "秘钥错误");
	    		map.put("state", "0");
	    		return map;
		    }
    	}
	}
	
	
	@RequestMapping(value = "pconearticle") //---3.PC端查看     单篇修改的文章详情
	@ResponseBody                                             
	public Map<Object,Object> onearticle(PgcArticle pa,String secret,String pust_val,HttpServletResponse response) throws IOException, FileUploadException {
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(secret==null||pust_val==null||pa.getId()==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
    	}else{
		    Md md=new Md();
		    String mds=md.md5calc(pust_val+"flower_duigu2016");
		    if(secret.endsWith(mds)){
		    			List<Object> list=new ArrayList<Object>();
						list=articleService.onearticle(pa);
						map.put("list", list);
						map.put("state", "1");
						map.put("message", "修改成功");
						return map; 
		    }else{
	    		map.put("message", "秘钥错误");
	    		map.put("state", "0");
	    		return map;
		    }
    	}
	}
	
	@RequestMapping(value = "listarticle")//---4.PC端显示 文章列表
	@ResponseBody                                             
	public Map<Object,Object> listarticle(PgcArticle pa,String secret,String pust_val,String time,Integer page,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pa.getUserId()==null||secret==null||pust_val==null||time==null||page==null){
    		map.put("message", "传入参数不能为空");
    		map.put("state", "0");
    		return map;
		    	}else{
			    Md md=new Md();
		    	String mds=md.md5calc(pust_val+"flower_duigu2016");
		    	if(secret.endsWith(mds)){
						List<Object> list=new ArrayList<Object>();
						list=articleService.listarticle(pa,page, time);
						long count=articleService.articlecount(pa);//---总条数
		    			int pagecount=(int) ((count-1)/20+1);  //总共多少页
		    			map.put("pagecount", pagecount);
						map.put("count",count);	
						map.put("list", list);	
						map.put("state", "1");
						map.put("message", "查詢文章列表成功");
						return map; 
		  	  }else{
	    			map.put("message", "秘钥错误");
	    			map.put("state", "0");
	    			return map;
		    }
    	}
	}
	
	
	@RequestMapping(value = "delarticle")//---5.删除PC端文章
	@ResponseBody                                             
	public Map<Object,Object> delarticle(PgcArticle pa,String secret,String pust_val,HttpServletResponse response){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object,Object>();
    	if(pa.getId()==null||secret==null||pust_val==null){
    		map.put("message", "参数不能为空");
    		map.put("state", "0");
    		return map;
		    	}else{
			    Md md=new Md();
		    	String mds=md.md5calc(pust_val+"flower_duigu2016");
		    	if(secret.endsWith(mds)){
							boolean b=articleService.delarticle(pa);
							if(b){
								map.put("state", "1");
								map.put("message", "删除文章成功");
								return map;
							}else{
								map.put("state", "0");
								map.put("message", "删除文章失败");
								return map;
							}
				  }else{
			    			map.put("message", "秘钥错误");
			    			map.put("state", "0");
			    			return map;
				 }
    	}
	}
    
    
    

}
